<?php

/**
 * Contrôleur des départements
 */
class departement_controller extends Component
{
    // Attributs
    const byPage = 10;
    const page_identifier = 'page-';
    public $_pageTotal;

    /**
     * Constructeur
     */
    public function __construct()
    {
    }

    /**
     * Données à envoyer à la vue par défaut
     */
    public function defaut()
    {
        if (isset($_GET['param'][0])) {
            if (preg_match("/" . self::page_identifier . "/i", $_GET['param'][0])) {
                $p = explode(self::page_identifier, $_GET['param'][0]);
                $this->getAll($p[1]);
            } else {
                $tmp = explode('-', $_GET['param'][0]);
                $this->getOne((int)$tmp[0]);
            }
        } else {
            $this->getAll();
        }
    }

    /**
     * Sélection d'un département
     *
     * @param unsigned $id L'id d'un département
     */
    private function getOne($id)
    {
        $data = Base::Load(CLASS_LOC)->getDepartement($id);
        $this->_view->assign('data', $data);
        $this->addBreadCrumb($data[3]);
        $this->_view->addBlock('content', 'departement.tpl');
    }

    /**
     * Récupération de tous les départements
     *
     * @param unsigned $page L'endroit du tableau à visualiser (pagination)
     */
    private function getAll($page = 1)
    {
        $this->_view->addBlock('autoCompleteDepartement','formAutocomplete.tpl');
        
        $dataTmp = Base::Load(CLASS_LOC)->getDepartement();
        $this->_pageTotal = ceil(count($dataTmp) / self::byPage) + 1;
        $start = self::byPage * $page - self::byPage;

        foreach ($dataTmp as $i => $d) {
            if ($i >= $start) {
                if ($i < $start + self::byPage) {
                    $d[1] = Base::Load(CLASS_LOC)->getRegion($d[1]);
                    $data[$i] = $d;
                } else {
                    break;
                }
            }
        }

        $this->_view->assign('pageTotal', $this->_pageTotal);
        $this->_view->assign('page', $page);
        $this->_view->assign('data', $data);

        if (isset($_GET['ajax']) && $_GET['ajax']) {
            $this->_view->assign('ajax', true);
            Base::Load(CLASS_BASE)->setLayout('vide.tpl');
            $this->_view->addBlock('data', 'list.tpl');
        } else {
            $this->_view->addBlock('content', 'list.tpl');
        }
    }

    public function getSelect()
    {
        $dataTmp = Base::Load(CLASS_LOC)->getDepartementByRegionID($_GET['param'][0]);
        exit(json_encode($dataTmp));
    }

    public function getAutoComplete()
    {
        $this->setContentType('text/plain');
        $query = $_GET['query'];
        $queryType = array('departement');
        $data = Base::Load(CLASS_SEARCH)->MySearch($query, $queryType, true);
        exit($data);
    }

    public function pdfList()
    {
        $data = Base::Load(CLASS_LOC)->getDepartement(null, true);
        $this->_view->assign('data',$data);
        Base::Load(CLASS_PDF)->simplePDF('pdfList.tpl', 'macrise.com-departements.pdf');
    }
}


/* -- Fin du fichier -- */